package cc.comac.mom.datahandlers.entity.config;

import cc.comac.mom.bean.config.EquipConnectConfigGroupBean;
import cc.comac.mom.bean.config.EquipConnectConfigParamBean;
import cc.comac.mom.dao.entity.config.EquipConnectConfigDao;
import cc.comac.mom.dao.entity.config.EquipConnectConfigGroupDao;
import cc.comac.mom.entity.config.EquipConnectConfigEntity;
import cc.comac.mom.entity.config.EquipConnectConfigGroupDetailEntity;
import cc.comac.mom.entity.config.EquipConnectConfigGroupEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Transactional
public class EquipConnectConfigGroupDataHandler extends ServiceImpl<EquipConnectConfigGroupDao, EquipConnectConfigGroupEntity> {
	@Autowired
	private EquipConnectConfigGroupDao equipConnectConfigGroupDao;
	@Autowired
	private EquipConnectConfigGroupDetailDataHandler equipConnectConfigGroupDetailDataHandler;
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;

	public ResultBean saveEquipConnectConfigGroup(Long connectId,List<EquipConnectConfigGroupBean> list) {
		ResultBean resultBean = new ResultBean(true);
		List<EquipConnectConfigGroupEntity> groupList=new ArrayList<>();
		List<EquipConnectConfigGroupDetailEntity> detailDataHandlerLsit=new ArrayList<>();

 		for(EquipConnectConfigGroupBean e:list){

			EquipConnectConfigGroupEntity temp=new EquipConnectConfigGroupEntity();
			temp.setConnectId(connectId);
			temp.setParamItemKey(e.getParamItemKey());
			baseEntityDataUtil.setBaseEntityData(temp);
			groupList.add(temp);
			for(EquipConnectConfigGroupDetailEntity detail:e.getDetailList()){
				detail.setGroupId(temp.getId());
				baseEntityDataUtil.setBaseEntityData(detail);
				detailDataHandlerLsit.add(detail);
			}
		}
		this.delete(connectId);
		Boolean insert = this.saveBatch(groupList);
		if (!insert) {
			throw new RuntimeException("新增失败");
		}
		equipConnectConfigGroupDetailDataHandler.saveBatch(detailDataHandlerLsit);
		resultBean.setObject(list);
		return resultBean;
	}

	public ResultBean delete(Long connectId) {
		ResultBean resultBean = new ResultBean(true);
		if (connectId == null) {
			throw new RuntimeException("数据为空");
		}
		QueryWrapper<EquipConnectConfigGroupEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.eq("connect_id",connectId);
		List<Long> groupIds=new ArrayList<>();
		List<EquipConnectConfigGroupEntity> list = equipConnectConfigGroupDao.selectList(queryWrapper);
		if (list != null&&list.size()>0) {
			for(EquipConnectConfigGroupEntity e:list){
				groupIds.add(e.getId());
				e.setRowStatus(0);
				baseEntityDataUtil.updateBaseEntityData(e);

			}
			this.updateBatchById(list);
			equipConnectConfigGroupDetailDataHandler.delete(groupIds);
		}
		return resultBean;
	}

	public ResultBean deleteByConnectIds(List<Long> connectIds) {
		ResultBean resultBean = new ResultBean(true);
		if (connectIds == null) {
			throw new RuntimeException("数据为空");
		}
		QueryWrapper<EquipConnectConfigGroupEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.in("connect_id",connectIds);
		List<Long> groupIds=new ArrayList<>();
		List<EquipConnectConfigGroupEntity> list = equipConnectConfigGroupDao.selectList(queryWrapper);
		if (list != null) {
			for(EquipConnectConfigGroupEntity e:list){
				groupIds.add(e.getId());
				e.setRowStatus(0);
				baseEntityDataUtil.updateBaseEntityData(e);

			}
			this.updateBatchById(list);
			equipConnectConfigGroupDetailDataHandler.delete(groupIds);
		}
		return resultBean;
	}
}
